<template>
	<view class="list" style="position: relative;">
		<view class="fixbg" :style="{backgroundImage:'url('+ data.sysFileInfo.fileUrl +')'}"></view>
		<scroll-view scroll-y="true">
			<view class="list-head">
				<view class="list-head-img">
					<image :src="data.sysFileInfo.fileUrl" mode=""></image>
				</view>
				<view class="list-head-text" style="line-height: 54rpx;">
					<view>{{ data.groupName }}</view>
					<view>{{ data.remark || '暂无简介' }}</view>
				</view>
			</view>
			<view v-if="data.router=='own'">
				<button class="list-share" open-type="share">
					<text class="iconfont iconicon-"></text><text>{{data.router == 'own'? '邀请微信好友':'申请加入圈子' }}</text>
				</button>
			</view>
			<view v-else>
				<button class="list-share" @click="apply" v-if="!isExist">
					<text class="iconfont iconicon-"></text><text>{{data.router == 'own'? '邀请微信好友':'申请加入圈子' }}</text>
				</button>
			</view>
			<view class="list-music">
				<view class="list-music-title">
					<u-icon name="account-fill" size="35"></u-icon>
					<text>圈子成员</text>
				</view>
				<scroll-view scroll-y="true">
					<view class="list-music-item" v-for="(item,index) in userList" :key="index"
						@tap="openActionList(item)">
						<view class="list-music-top">{{ index + 1 }}</view>
						<view class="list-music-song">
							<u-avatar :src="baseURL+ item.avatar" size="mini"></u-avatar>
							<view style="display: flex;justify-content: space-between;">
								<text>{{ item.nickName }}</text>
								<text>{{currentUserId == item.userId? '我':(item.userId == data.createBy?'管理员':'成员' )}}</text>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</scroll-view>
		<u-action-sheet :list="actionList" v-model="showActionSheet" :cancel-btn="true"
			@click="settingOfOption"></u-action-sheet>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		getOneOfUser,
		getOneOfReview,
		addGroupInvite,
		removeGroupInvite
	} from '@/api/bot/groupInvite'
	import '@/common/iconfont.css'
	import config from '@/config'
	export default {
		data() {
			return {
				data: {},
				userList: [],
				baseURL: config.baseUrl,
				actionList: [{
						text: '仅移除成员',
						color: "#fa3534"
					},
					{
						text: '移除成员及数据',
						color: "#fa3534"
					}
				],
				showActionSheet: false,
				currentObj: '',
				currentUserId: '',
				isExist: false
			}
		},
		onLoad(options) {
			this.currentUserId = this.$store.getters.userId;
			const formDataStr = decodeURIComponent(options.data);
			this.data = JSON.parse(formDataStr);
			this.getOneOfUser()
		},
		methods: {
			getOneOfUser() {
				let query = {
					createBy: this.data.createBy,
					groupId: this.data.id
				}
				getOneOfUser(query).then(res => {
					res.rows.forEach(element => {
						if (element.userId == this.data.createBy) {
							element.level = 2;
						} else if (element.userId == this.currentUserId) {
							element.level = 1;
						} else {
							element.level = 3;
						}
					})
					this.userList = res.rows.sort((a, b) => a.level - b.level);
					this.isExist = this.userList.some(user => user.userId === this.currentUserId);
					console.log(this.isExist);
				})
			},
			openActionList(obj) {
				this.currentObj = obj
				if (this.data.router == 'own' && this.data.createBy == this.currentUserId && this.data.createBy != obj
					.userId) {
					this.showActionSheet = true
				}
			},
			settingOfOption(index) {
				console.log(`点击了第${index + 1}项，内容为：${this.actionList[index].text}`)
				if (index == 0) {
					this.$modal.confirm('是否移除该成员呢？').then(() => {
						this.removeGroupInvite(false)
					});
				} else {
					this.$modal.confirm('是否移除该成员及数据呢？').then(() => {
						this.removeGroupInvite(true)
					});
				}
			},
			apply() {
				this.$modal.confirm('是否申请加入呢？').then(() => {
					this.addGroupInvite()
				});
			},
			removeGroupInvite(isClear) {
				let data = {
					groupId: this.data.id,
					inviteUserId: this.currentObj.userId,
					isClear: isClear
				}
				removeGroupInvite(data).then(res => {
					if (res.code == 200) {
						this.$modal.msgSuccess("移除成功")
						this.getOneOfUser()
					} else {
						this.$modal.msgError("移除失败")
					}
				})
			},
			addGroupInvite() {
				let formData = {
					status: 1,
					groupId: this.data.id
				}
				getOneOfReview(formData).then(res => {
					if (res.data === 0) {
						addGroupInvite(formData).then(res => {
							if (res.code == 200) {
								this.$refs.uToast.show({
									title: `等待管理员审核`,
									type: 'success',
								})
								this.getOneOfUser()
							} else {
								this.$modal.msgError("申请失败")
							}
						})
					} else {
						this.$refs.uToast.show({
							title: `已加急催促管理员`,
							type: 'warning',
						})
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.list-head {
		display: flex;
		margin: 30rpx;
	}

	.list-head-img {
		width: 265rpx;
		height: 265rpx;
		border-radius: 15rpx;
		margin-right: 40rpx;
		overflow: hidden;
		position: relative;
	}

	.list-head-img image {
		width: 100%;
		height: 100%;
	}

	.list-head-text {
		flex: 1;
		font-size: 24rpx;
		color: #a7b3c4;
	}

	.list-head-text image {
		width: 52rpx;
		height: 52rpx;
		border-radius: 50%;
	}

	.list-head-text view:nth-child(1) {
		font-size: 34rpx;
		color: #000;
	}

	.list-head-text view:nth-child(2) {
		display: flex;
		align-items: center;
		margin: 30rpx 0;
	}

	.list-head-text view:nth-child(2) text {
		margin-left: 15rpx;
	}

	.list-head-text view:nth-child(3) {
		line-height: 38rpx;
	}

	.list-share {
		width: 330rpx;
		height: 72rpx;
		margin: 0 auto;
		background: rgba(0, 0, 0, 0.4);
		text-align: center;
		line-height: 72rpx;
		font-size: 26rpx;
		color: white;
		border-radius: 36rpx;
	}

	.list-share text {
		margin-right: 15rpx;
	}

	.list-music {
		height: 100vh;
		background: white;
		border-radius: 50rpx;
		overflow: hidden;
		margin-top: 45rpx;
	}

	.list-music-title {
		height: 58rpx;
		line-height: 58rpx;
		margin: 30rpx 30rpx 70rpx 30rpx;
	}

	.list-music-title text:nth-child(1) {
		font-size: 29rpx;
	}

	.list-music-title text:nth-child(2) {
		font-size: 34rpx;
		margin: 0 10rpx 0 25rpx;
	}

	.list-music-title text:nth-child(3) {
		font-size: 28rpx;
		color: #b2b2b2;
	}

	.list-music-item {
		display: flex;
		margin: 0 30rpx 70rpx 44rpx;
		align-items: center;
	}

	.list-music-top {
		width: 56rpx;
		font-size: 28rpx;
		color: #979797;
	}

	.list-music-song {
		display: flex;
		align-items: center;
		flex: 1;
		line-height: 40rpx;
	}

	.list-music-song view:nth-child(2) {
		margin-left: 15rpx;
		font-size: 30rpx;
		width: 70vw;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.list-music-song image {
		width: 34rpx;
		height: 22rpx;
		margin-right: 10rpx;
	}

	.list-music-item text {
		font-size: 30rpx;
		color: #979797;
	}
</style>